package com.jili.redistest.controller;

import com.jili.redistest.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author: Jili
 * @date: Created on 2021/7/9 17:21
 */
@RestController
public class StudentController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/test")
    public String getTest() {
        return "Test!";
    }

    //字符串

    @PostMapping("/setRedis")
    public void setRedis(@RequestBody Student student) {
        redisTemplate.opsForValue().set("student", student);
    }

    @GetMapping("/setRedisS")
    public void setRedisS() {
        Student student=new Student();
        student.setId(0001);
        student.setName("test");
        redisTemplate.opsForValue().set("student", student);
    }

    @GetMapping("/getRedis/{key}")
    public String getRedis(@PathVariable("key") String key) {

        Object object = redisTemplate.opsForValue().get(key);

        return String.valueOf(object);
    }

    @GetMapping("/getS")
    public String getS() {

        Object object = redisTemplate.opsForValue().get("name");

        return String.valueOf(object);
    }

    @DeleteMapping("/delete/{key}")
    public boolean delete(@PathVariable("name") String key){
        redisTemplate.delete(key);
        return redisTemplate.hasKey(key);
    }

    //列表

    @GetMapping("/list")
    public List<String>listTest(){
        ListOperations <String,String> listOperations = redisTemplate.opsForList();
        listOperations.leftPush("HellList","Hello1");
        listOperations.leftPush("HellList","Hello2");
        listOperations.leftPush("HellList","Hello3");
        List<String> list = listOperations.range("HellList", 0, 2);
        return list;
    }

    //集合
    @GetMapping("/set")
    public Set<String> SetTest(){
        SetOperations <String,String>setOperations = redisTemplate.opsForSet();
        setOperations.add("HellSet","Set1");
        setOperations.add("HellSet","Set1");
        setOperations.add("HellSet","Set2");
        setOperations.add("HellSet","Set2");
        setOperations.add("HellSet","Set3");
        return setOperations.members("HellSet");
    }

    //有序集合
    @GetMapping("/zset")
    public Set<String> zSetTest(){
        ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset","Hell",1);
        zSetOperations.add("zset","World",2);
        zSetOperations.add("zset","World",2);
        zSetOperations.add("zset","Java",3);
        Set<String> zset = zSetOperations.range("zset",0,2);
        return zset;
    }

    /**
     *  哈希
     *  HashMap key value
     *  HashOperations key hashkey value
     */
    @GetMapping("/hash")
    public String hashTest(){
        HashOperations <String,String,String>hashOperations = redisTemplate.opsForHash();
        hashOperations.put("key1","hashkey","hell");
        hashOperations.put("key2","hashkey2","hell2");
        String s = hashOperations.get("key1", "hashkey");
        HashMap hashMap=new HashMap();
        return s;
    }



}
